//
//  HJInformationOperateView.m
//  KaiChat
//
//  Created by lx on 2024/3/11.
//

#import "HJInformationOperateView.h"
#import "HJButton.h"

@interface HJInformationOperateView()

@property (nonatomic, strong) NSArray *types;
@property (nonatomic, strong) NSMutableArray *typeButtons;

@end

@implementation HJInformationOperateView

- (instancetype)initWithTypes:(NSArray *)types {
    if (self = [super init]) {
        _types = types;
        
        for (id  _Nonnull obj in types) {
            
            HJButton *itemButton = [HJButton buttonWithSuperView:self font:UIFontMake(12) color: UIColor.tj_868391Color title:nil];
            itemButton.anyObject = obj;
            itemButton.imagePosition = QMUIButtonImagePositionLeft;
            itemButton.spacingBetweenImageAndTitle = 6;
            [itemButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            [itemButton addTarget:self action:@selector(didClickItemButton:) forControlEvents:UIControlEventTouchUpInside];
            [self.typeButtons addObject:itemButton];
            
            if ([obj intValue] == HJInformationOperateLike) {
                _likeButton = itemButton;
                [_likeButton setImage:[UIImage imageNamed:@"informationOperate_like"] forState:UIControlStateNormal];
                [_likeButton setImage:[UIImage imageNamed:@"like_select_icon"] forState:UIControlStateSelected];
            } else if ([obj intValue] == HJInformationOperateShare) {
                _shareButton = itemButton;
                [_shareButton setImage:[UIImage imageNamed:@"informationOperate_share"] forState:UIControlStateNormal];
            } else if ([obj intValue] == HJInformationOperateViews) {
                _viewsButton = itemButton;
                [_viewsButton setImage:[UIImage imageNamed:@"informationOperate_views"] forState:UIControlStateNormal];
            } else if ([obj intValue] == HJInformationOperateComment) {
                _commentButton = itemButton;
                [_commentButton setImage:[UIImage imageNamed:@"informationOperate_comment"] forState:UIControlStateNormal];
            }
        }
    
    }
    return self;
}

- (NSMutableArray *)typeButtons {
    if (!_typeButtons) {
        _typeButtons = [NSMutableArray array];
    }
    return _typeButtons;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat W = (self.frame.size.width) / self.typeButtons.count;
    
    [self.typeButtons enumerateObjectsUsingBlock:^(HJButton *  _Nonnull itemButton, NSUInteger idx, BOOL * _Nonnull stop) {
        itemButton.frame = CGRectMake(idx * W, 0, W, self.frame.size.height);
    }];
}

- (void)didClickItemButton:(HJButton *)button {
    if (_didClickButtonBlock) {
        _didClickButtonBlock(button, [button.anyObject integerValue]);
    }
    
}



@end
